---
title: "Experiment 3b"
output: word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r load libraries}

```

```{r import}
library(tidyverse)

d <- read.csv("exp3b_rawdata.csv")

a <- d

## need to get the list of columns with this in the name: comp3a

compAs <- grep("comp3",names(d))
d <- a %>% select(-c(1:8,10:17,all_of(compAs))) %>% 
  pivot_longer(2:1729, "question","response",values_drop_na=T) %>%
  filter(bot1==1 & bot2==2) %>%
  separate(question,into = c("scenario","options","orderInfo","question","XX"),sep="_") %>%
  mutate(scenario = as.numeric(gsub("X","",scenario))
         ,altQuality = case_when(
           str_detect(options,"LLH") ~ "Normal Alternative"
           ,str_detect(options,"LHH") ~ "Abnormal Alternative"
         )
         ,questionKind = case_when(
           str_detect(question,"force") ~ "hadTo"
           ,str_detect(question,"norm") ~ "unusualness"
           ,str_detect(question,"val") ~ "goodIdea"
           ,str_detect(question,"comp1") ~ "comprehension1"
           ,str_detect(question,"comp2") ~ "comprehension2"
         )
         ,target = case_when(
            str_detect(question,"actnorm") ~ "actual"
           ,str_detect(question,"actval") ~ "actual"
           ,str_detect(question,"norm1") ~ "alt1"
           ,str_detect(question,"norm2") ~ "alt2"
           ,str_detect(question,"val1") ~ "alt1"
           ,str_detect(question,"val2") ~ "alt2"
         )
         ) %>%
  select(-c(options:XX)) %>%
  pivot_wider(names_from = c(questionKind,target),values_from=value) %>%
  filter(comprehension1_NA==2 & comprehension2_NA==3) %>%
  rowwise() %>% ## Min/Max works here because it just takes the one value for the actual
  mutate(Value = max(goodIdea_alt1,goodIdea_alt2,goodIdea_actual,na.rm = T)
         ,Norm = min(unusualness_alt1,unusualness_alt2,unusualness_actual,na.rm=T)
        ) %>%
  rename(Force=hadTo_NA) 

d$Norm[d$Norm==Inf] <- NA # one sub didn't give a value for norm resulting in Inf from the Min
d$altQuality <- factor(d$altQuality, levels = c("Normal Alternative", "Abnormal Alternative"))

#only keep participants with complete data
d <- d %>% drop_na(Norm)
d <- d %>% drop_na(Value)
cleandata <- d

actdata <- cleandata[which(cleandata$condition == "Actual"),] ##questions about actual action
altdata <- cleandata[which(cleandata$condition == "Alternative"),] ##questions about alternative action

write.csv(d, "exp3b_cleandata.csv")
```
##Get descriptives for Actual Judgment Condition
```{r descriptives}
library(readr)
library(reshape2)
library(plyr)
library(ggplot2)
library(mediation)
library(stringr)

#get descriptives
force_bycond_desc <- ddply(cleandata, c( "condition", "altQuality"), summarise,
               N    = length(Force),
               mean = mean(Force),
               sd   = sd(Force),
               se   = sd / sqrt(N)
)
print(force_bycond_desc)

force_desc <- ddply(cleandata, c("altQuality"), summarise,
               N    = length(Force),
               mean = mean(Force),
               sd   = sd(Force),
               se   = sd / sqrt(N)
)
print(force_desc)

val_desc <- ddply(cleandata, c("condition", "altQuality"), summarise,
               N    = length(Value),
               mean = mean(Value),
               sd   = sd(Value),
               se   = sd / sqrt(N)
)
print(val_desc)

norm_desc <- ddply(cleandata, c("condition", "altQuality"), summarise,
               N    = length(Norm),
               mean = mean(Norm),
               sd   = sd(Norm),
               se   = sd / sqrt(N)
)
print(norm_desc)

character_desc <- ddply(cleandata, c("altQuality"), summarise,
               N    = length(character_1),
               mean = mean(character_1),
               sd   = sd(character_1),
               se   = sd / sqrt(N)
)
print(character_desc)

character_bycond_desc <- ddply(cleandata, c("condition", "altQuality"), summarise,
               N    = length(character_1),
               mean = mean(character_1),
               sd   = sd(character_1),
               se   = sd / sqrt(N)
)
print(character_bycond_desc)


```
##Final Analyses 
```{r testing effect of condition echo=FALSE}
mosaic::cor.test(Value ~ Norm, data = cleandata[cleandata$condition == "Alternative",])

#Subject level analysis: Predict force judgments by condition. 
summary(lm(Force ~ altQuality, data=cleandata))

#predicting character judgments by whether or not there was a normal alternative
summary(lm(character_1 ~ altQuality, data = cleandata))

#predicting preceptions of the actual actions by condition
summary(lm(Norm ~ altQuality, data = cleandata[cleandata$condition == "Actual",]))
summary(lm(Value ~ altQuality, data = cleandata[cleandata$condition == "Actual",]))

# Run full model with the critical alternative actions without condition
alt_mod <- (lm(Force ~ Norm + Value, data = cleandata[cleandata$condition == "Alternative",]))
summary(alt_mod)
summary(lm(Force ~ Norm + Value, data = cleandata[cleandata$condition == "Actual",]))

#compute variance inflation factor for altNorm and altValue
car::vif(alt_mod)

#predict force from just altNorm and just altVal
summary(lm(Force ~ Norm, data = cleandata[cleandata$condition == "Alternative",]))
summary(lm(Force ~ Value, data = cleandata[cleandata$condition == "Alternative",]))

#Are the actual normality judgments in both conditions significantly below 50?
#In other words, are the actual actions always viewed as very normal? 
mosaic::t.test(~Norm, mu = 50, data = actdata[actdata$altQuality == "Normal Alternative",])
mosaic::t.test(~Norm, mu = 50, data = actdata[actdata$altQuality == "Abnormal Alternative",])

#Are the actual value judgments in both conditions significantly above 50?
#In other words, are the actual actions always viewed as very good? 
mosaic::t.test(~Value, mu = 50, data = actdata[actdata$altQuality == "Normal Alternative",])
mosaic::t.test(~Value, mu = 50, data = actdata[actdata$altQuality == "Abnormal Alternative",])

#Are the character judgments in both conditions significantly above 50?
mosaic::t.test(~character_1, mu = 50, data = d[d$altQuality == "Normal Alternative",])
mosaic::t.test(~character_1, mu = 50, data = d[d$altQuality == "Abnormal Alternative",])

```


